package edu.jd.movieplatform.security.m_home.impl;

import edu.jd.movieplatform.common.CurrentManager;
import edu.jd.movieplatform.security.m_home.MHomeService;
import edu.jd.movieplatform.security.m_home.MModule;
import edu.jd.movieplatform.security.m_home.MenuVO;
import edu.jd.movieplatform.security.m_home.dao.MHomeDao;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.awt.*;
import java.util.ArrayList;
import java.util.List;

@Service @Transactional
public class MHomeServiceImpl implements MHomeService {

    @Resource
    private MHomeDao mHomeDao;

    @Override
    public List<MenuVO> getMenus(CurrentManager currentManager) {

        List<MModule> list = mHomeDao.findModuleListByManager(currentManager.getM_status(),currentManager.getM_id());

        List<MenuVO>  menuVOList = new ArrayList<>();
        MenuVO menu = null;//当前的菜单

        for (MModule mModule : list){
            if(menu == null || !menu.getId().equals(mModule.getP_id())){
                menu = new MenuVO();
                menu.setId(mModule.getP_id());
                menu.setName(mModule.getP_name());
                menu.setChildren(new ArrayList<MenuVO>());

                menuVOList.add(menu);
            }

            MenuVO child = new MenuVO();
            child.setId(mModule.getMd_id());
            child.setName(mModule.getMd_name());
            child.setUrl(mModule.getMd_web_route());

            menu.getChildren().add(child);
        }

        return menuVOList;
    }

    @Override
    public void setAvatar(CurrentManager currentManager, String filename) {
            mHomeDao.updateAvatar(currentManager.getM_id(),filename);
    }
}
